<template>
  <div class="div">
    <van-nav-bar title="Star 头条" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="user.mobile"
        required
        name="mobile"
        label="手机号"
        placeholder="手机号"
        :rules="[{ required: true, message: '请填写正确手机号' ,pattern:/^1[3-9]\d{9}$/ }]"
      />
      <van-field
        v-model="user.code"
        required 
        type="password"
        name="code"
        label="验证码"
        placeholder="验证码"
        :rules="[{ required: true, message: '请填写验证码',pattern:/^\d{6}$/ }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit" :disabled="loading" :loading="loading" loading-text="加载中..."
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import {login} from '../../api/index'
import { Toast } from 'vant';
import {setToken} from '../../utils/token'
export default {
  data() {
    return {
      user: {
        mobile: "13888888888",
        code: "246810",
      },
      loading:false,
    };
  },
  methods: {
    async onSubmit(values) {
     this.loading=true
      try {
        let res=await login(this.user)
        setToken(res.data.data.token)
        Toast.success('登录成功');
        this.$router.replace({
          path:'/layout/home'
        })
      } catch (error) {
        Toast.fail('手机号或验证码不正确');
      }
      this.loading=false
    },
  },
};
</script>

<style scoped lang="less">
</style>